<template>
  <el-dialog
    title="定时器"
    :visible="dialogVisible"
    width="50%"
    @open="open"
    @close="close"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="ruleForm"
      label-width="120px"
      class="demo-ruleForm"
      size="small"
    >
      <el-form-item label="触发器:" prop="scheduleToken">
        <el-select
          v-model="form.scheduleToken"
          placeholder="请选择"
          class="w100"
        >
          <el-option
            v-for="item in scheduleList"
            :key="item.token"
            :label="item.name"
            :value="item.token"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="定时器类型:" prop="jobType">
        <el-select v-model="form.jobType" placeholder="请选择" class="w100">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="定时器状态" prop="jobStateShow">
        <el-input disabled v-model="form.jobStateShow"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button
        :loading="btnLoading"
        type="primary"
        @click="sumbit"
        size="small"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { postJobs, putJobs } from "@/api/schedules";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    scheduleList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      btnLoading: false,
      options: [
        {
          label: "能源能耗",
          value: "EnergyConsumption"
        },
        {
          label: "命令调用",
          value: "CommandInvocation"
        },
        {
          label: "批量命令调用",
          value: "BatchCommandInvocation"
        }
      ],
      form: {
        scheduleToken: "",
        jobStateShow: "未开始",
        jobState: "Unsubmitted",
        jobType: "CommandInvocation",
        jobConfiguration: {}
      },
      rules: {
        scheduleToken: [{ required: true, message: "请选择触发器" }],
        jobType: [{ required: true, message: "请选择定时器类型" }]
      }
    };
  },
  methods: {
    open() {},
    sumbit() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          this.btnLoading = true;
          console.log("this.form  :>> ", this.form);
          try {
            if (!this.form.token) {
              await postJobs({ ...this.form });
            } else {
              await putJobs({ ...this.form });
            }
            this.btnLoading = false;
            this.close(true);
            this.$msg({
              type: "success",
              message: "提交成功"
            });
          } catch (error) {
            this.btnLoading = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    close(isReset) {
      // this.form = {
      //   scheduleToken: "",
      //   jobState: "Unsubmitted",
      //   jobType: "CommandInvocation",
      //   jobConfiguration: {}
      // };
      this.$refs["ruleForm"].resetFields();
      this.$emit("update:dialogVisible", false);
      this.$emit("close", isReset);
    }
  }
};
</script>

<style></style>
